//
//  WBN_FM_PlayViewController.m
//  GoodManNews
//
//  Created by 王博男 on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WBN_FM_PlayViewController.h"
#import "STKAudioPlayer.h"
#import "GoodManNews_AFNetTool.h"
#import "Masonry.h"
#import "WBN_FM_DetailView.h"
#import "WBN_FM_DetailTableViewCell.h"
#import "WBN_FM_DetailModel.h"
#import "WBN_FM_DetailAudioModel.h"
#import "UIImageView+WebCache.h"


#define PLAYURL [NSString stringWithFormat:@"http://api.3g.ifeng.com/api_fm_resource?rid=%@&page=1&pageSize=20&gv=4.6.9&av=0&proid=ifengnews&os=ios_9.2&vt=5&screen=750x1334&publishid=4002&uid=bf668f34bb83eff6208cbc233768f3ea93644221", self.rid]

@interface WBN_FM_PlayViewController () <UITableViewDataSource, UITableViewDelegate, WBN_FM_DetailViewDelegate, WBN_FM_DetailTableViewCellDelegate, NSURLSessionDownloadDelegate>


@property (nonatomic, retain) UIView *headV;
@property (nonatomic, retain) UIImageView *headImageV;

@property (nonatomic, retain) UIView *titleV;
@property (nonatomic, retain) UILabel *titleL;

@property (nonatomic, retain) UIImageView *playerImageV;
@property (nonatomic, retain) UILabel *leftTimeL;
@property (nonatomic, retain) UILabel *rightTimeL;
@property (nonatomic, retain) UIButton *leftB;
@property (nonatomic, retain) UIButton *rightB;
@property (nonatomic, retain) UIButton *playB;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, retain) WBN_FM_DetailView *detailV;
@property (nonatomic, retain) UITableView *tableV;

@property (nonatomic, retain) WBN_FM_DetailModel *model;
@property (nonatomic, retain) NSMutableArray *arr;

@property (nonatomic, retain) UILabel *detailLabel;

// 当前播放
@property (nonatomic, retain) WBN_FM_DetailAudioModel *currentModel;
// 记录当前播的是第一个
@property (nonatomic, assign) NSInteger count;

@property (nonatomic, retain) NSTimer *topTimer;

@property (nonatomic, copy) NSString *currentPlayId;
@property (nonatomic, assign) BOOL countBool;

@property (nonatomic, retain) WBN_FM_DetailAudioModel *downLoadModel;

// 记录点击下载的是哪个cell
@property (nonatomic, assign) NSInteger buttonIndex;
// 本地音乐
@property (nonatomic, retain) NSMutableArray *localArr;

@property (nonatomic, retain) NSURLSessionDownloadTask *downLoadT;
// 记录下载状态
@property (nonatomic, assign) NSInteger downLoad;


@end

@implementation WBN_FM_PlayViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"下载" object:nil];
    [super dealloc];
}

+(instancetype)sharePlayerViewController
{
    static WBN_FM_PlayViewController *playerVC = nil;
    if (nil == playerVC) {
        playerVC = [[WBN_FM_PlayViewController alloc] init];
    }
    return playerVC;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    if (NO == self.countBool) {
        
        self.countBool = YES;
        
    } else {
        
        if (![self.currentPlayId isEqualToString:self.rid]) {
            
            [self.player stop];
            [self.timer invalidate];
            
            self.currentPlayId = self.rid;
            
            [self.titleL removeFromSuperview];
            [self.arr removeAllObjects];
            
            [self getData];
            [self resumeLayer:self.playerImageV.layer];
            
        } else if (STKAudioPlayerStatePlaying == self.player.state) {
            
            [self layoutAudioImageView];
            
        } else {
            
            [self createCABasicAnimation];
            
            [self pauseLayer:self.playerImageV.layer];
            
        }
        
    }

    self.currentPlayId = self.rid;
    
    
    [self getLocalMusic];
}

- (void)getLocalMusic
{
    self.localArr = [NSMutableArray array];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    // 找出已下载的.mp3
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            if ([fileName  hasSuffix:@".mp3"]) {
                
                [self.localArr addObject:fileName];
                
            }
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.arr = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createAudioView];
    [self createTableView];

    NSLog(@"%@", NSHomeDirectory());
    [self getData];

}

- (void)createAudioView
{
    self.headImageV = [UIImageView new];
    [self.view addSubview:self.headImageV];
    
    
    // 毛玻璃
    UIVisualEffectView *effectV = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    [self.headImageV addSubview:effectV];
    
    [effectV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
        
    }];
    
    
    self.headV = [UIView new];
    self.headV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.headV];
    
    [self.headV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.height.equalTo(@300);
        make.width.equalTo(self.view);
        
    }];
    
    
    self.titleV = [UIView new];
    [self.view addSubview:self.titleV];
    
    [self.titleV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.headV).offset(30);
        make.left.equalTo(self.headV);
        make.height.equalTo(@40);
        make.width.equalTo(self.headV);
        
    }];
    
    
    self.leftTimeL = [UILabel new];
    [self.headV addSubview:self.leftTimeL];
    self.leftTimeL.textColor = [UIColor whiteColor];
    
    [self.leftTimeL mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.headV.mas_bottom).offset(-90);
        make.left.equalTo(self.headV).offset(20);
        make.height.equalTo(@20);
        make.width.equalTo(@60);
        
    }];
    
    
    self.rightTimeL = [UILabel new];
    [self.headV addSubview:self.rightTimeL];
    self.rightTimeL.textColor = [UIColor whiteColor];
    
    [self.rightTimeL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headV.mas_bottom).offset(-90);
        make.right.equalTo(self.headV).offset(-20);
        make.height.equalTo(@20);
        make.width.equalTo(@60);
        
    }];
    
    
    self.slider = [UISlider new];
    self.slider.minimumTrackTintColor = [UIColor whiteColor];
    [self.slider setThumbImage:[UIImage imageNamed:@"anniu"] forState:UIControlStateNormal];
    [self.slider addTarget:self action:@selector(sliderAction) forControlEvents:UIControlEventValueChanged];
    [self.headV addSubview:self.slider];
    
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.leftTimeL.mas_top);
        make.left.equalTo(self.leftTimeL.mas_right).with.offset(10);
        make.right.equalTo(self.rightTimeL.mas_left).with.offset(-10);
        make.height.equalTo(@20);
        
    }];
    
    
    self.leftB = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftB setImage:[UIImage imageNamed:@"fm_before_night@3x"] forState:UIControlStateNormal];
    [self.leftB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headV addSubview:self.leftB];
    
    [self.leftB mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.headV.mas_bottom).offset(-50);
        make.left.equalTo(self.headV).offset(80);
        make.height.equalTo(@30);
        make.width.equalTo(@30);
        
    }];
    
    
    self.rightB = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightB setImage:[UIImage imageNamed:@"fm_next_night@3x"] forState:UIControlStateNormal];
    [self.rightB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headV addSubview:self.rightB];
    
    [self.rightB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headV.mas_bottom).offset(-50);
        make.right.equalTo(self.headV).offset(-80);
        make.height.equalTo(@30);
        make.width.equalTo(@30);
        
    }];
    
    
    self.playB = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.playB setImage:[UIImage imageNamed:@"fm_play_night@3x"] forState:UIControlStateNormal];
    [self.playB setImage:[UIImage imageNamed:@"fm_pause_night@3x"] forState:UIControlStateSelected];
    [self.playB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headV addSubview:self.playB];
    
    [self.playB mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.headV);
        make.top.equalTo(self.headV.mas_bottom).offset(-55);
        make.height.equalTo(@40);
        make.width.equalTo(@40);
        
    }];

    
    
    self.playerImageV = [UIImageView new];
    self.playerImageV.layer.cornerRadius = 55;
    [self.headV addSubview:self.playerImageV];
    
    
    [self.playerImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.headV);
        make.top.equalTo(self.headV).offset(80);
        make.height.equalTo(@110);
        make.width.equalTo(@110);
        
    }];
    
}


- (void)layoutAudioView
{
    [self.headImageV sd_setImageWithURLStr:self.currentModel.image_big placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    
    [self.headImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
        
    }];
    
    
    self.leftTimeL.text = @"00:00";
    self.rightTimeL.text = @"00:00";
    
    
    
    self.titleL = [UILabel new];
    [self.headV addSubview:self.titleL];
    self.titleL.textColor = [UIColor whiteColor];
    self.titleL.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    self.titleL.text = self.currentModel.title;
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headV).offset(30);
        make.height.equalTo(@40);
        
        [self getWidth];
        
    }];

    
    self.topTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
    
    
    [self.playerImageV sd_setImageWithURLStr:self.currentModel.image placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    
    
   
}

// 图片动画
- (void)layoutAudioImageView
{
    self.playerImageV = [UIImageView new];
    self.playerImageV.layer.cornerRadius = 55;
    [self.headV addSubview:self.playerImageV];
    [self.playerImageV sd_setImageWithURLStr:self.currentModel.image placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    
    
    [self.playerImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.headV);
        make.top.equalTo(self.headV).offset(80);
        make.height.equalTo(@110);
        make.width.equalTo(@110);
        
    }];
    
    [self createCABasicAnimation];
}

// 自适应宽度
- (void)getWidth
{
    CGRect frame = [self.titleL.text boundingRectWithSize:CGSizeMake(1000, 35) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16.0] forKey:NSFontAttributeName] context:nil];
    

    [self.titleL mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(@(frame.size.width));
        
        if (frame.size.width <= [[UIScreen mainScreen] bounds].size.width) {
            
            make.centerX.equalTo(self.titleV);
            
        }
        
    }];
    
}


- (void)buttonAction:(UIButton *)sender
{
    if (sender == self.playB) {
        
        if (sender == self.playB) {
            
            if (STKAudioPlayerStatePlaying == self.player.state) {
                
                [self.player pause];
                self.playB.selected = NO;
                
                [self pauseLayer:self.playerImageV.layer];
                
            } else if (STKAudioPlayerStatePaused == self.player.state) {
                
                [self.player resume];
                self.playB.selected = YES;
                
                [self resumeLayer:self.playerImageV.layer];
                
            } else if (STKAudioPlayerStateStopped == self.player.state) {
                
                self.count = 0;
                
                self.currentModel = [self.arr objectAtIndex:self.count];
                
                self.titleL.text = self.currentModel.title;
                [self getWidth];
                
                [self createPlayer];
                
            }
            
        }
        
    } else if (sender == self.rightB) {
        
        if (self.count == self.arr.count - 1) {
            
            [self showAlert:@"当前已经是最后一个节目了"];
            
        } else if (self.count == self.arr.count) {
            
        } else {
            
            [self.player stop];
            [self.timer invalidate];
            
            self.count++;
            self.currentModel = [self.arr objectAtIndex:self.count];
            
            self.titleL.text = self.currentModel.title;
            [self getWidth];
            
            [self createPlayer];
            
        }
        
    } else {
        
        if (0 == self.count) {
            
            [self showAlert:@"当前已经是第一个节目了"];
            
        } else {
            
            [self.player stop];
            [self.timer invalidate];
            
            self.count--;
            self.currentModel = [self.arr objectAtIndex:self.count];
            
            self.titleL.text = self.currentModel.title;
            [self getWidth];
            
            [self createPlayer];
            
        }
        
    }
}


- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}


- (void)showAlert:(NSString *) _message//时间
{
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:nil message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}


- (void)timerAction:(NSTimer *)sender
{
    if (sender == self.timer) {
        
        self.leftTimeL.text = [NSString stringWithFormat:@"%02ld:%02ld", (NSInteger)self.player.progress / 60, (NSInteger)self.player.progress % 60];
        
        self.rightTimeL.text = [NSString stringWithFormat:@"%02ld:%02ld", (NSInteger)self.player.duration / 60, (NSInteger)self.player.duration % 60];
        
        self.slider.maximumValue = self.player.duration;
        self.slider.value = self.player.progress;
        
        
        // 自动播放下一首
        if (STKAudioPlayerStateStopped == self.player.state) {
            
            [self.player stop];
            [self.timer invalidate];
            
            if (self.count < self.arr.count - 1) {
                
                self.count++;
                self.currentModel = [self.arr objectAtIndex:self.count];
                
                self.titleL.text = self.currentModel.title;
                [self getWidth];
                
                [self createPlayer];
                
            } else {
                
                self.count++;
                
                [self layoutAudioImageView];
                [self pauseLayer:self.playerImageV.layer];
                self.playB.selected = NO;
                
            }
            
        }
        
    }
}

- (void)sliderAction
{
    [self.player seekToTime:self.slider.value];
}

- (void)createPlayer
{
    STKAudioPlayerOptions playerOptions = {YES, YES, {50, 100, 200, 400, 800, 1600, 2600, 16000}};
    self.player = [[[STKAudioPlayer alloc] initWithOptions:playerOptions] autorelease];
    [self.player play:self.currentModel.filePath];
    
    self.playB.selected = YES;
    
    [self createCABasicAnimation];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

// 创建动画
- (void)createCABasicAnimation
{
    CABasicAnimation *basicA = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    basicA.fromValue = @(0);
    basicA.toValue = @(2 * M_PI);
    basicA.duration = 70.f;
    basicA.repeatCount = NSIntegerMax;
    
    [self.playerImageV.layer addAnimation:basicA forKey:@"aaa"];
}

//暂停layer上面的动画
- (void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}


//继续layer上面的动画
- (void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor clearColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    self.tableV.rowHeight = 120;
    
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.headV.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
        make.left.equalTo(self.view);
        make.width.equalTo(self.view);
        
    }];
}

// 头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.detailV = [WBN_FM_DetailView new];
    self.detailV.delegate = self;
    [self.detailV.imageV sd_setImageWithURLStr:self.currentModel.image placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    
    self.detailV.titleL.text = self.model.programName;
    self.detailV.contentL.text = self.model.programDetails;
    
    if (0 == self.model.programDetails.length) {
        self.detailV.contentL.text = @"暂无简介";
        self.detailV.button.hidden = YES;
    }
    
    self.detailV.backgroundColor = [UIColor whiteColor];
    
    self.tableV.tableHeaderView = self.detailV;
    
    [self.detailV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.tableV);
        make.left.equalTo(self.tableV);
        make.width.equalTo(self.tableV);
        make.height.equalTo(@80);
        
    }];
    
    return self.detailV;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    WBN_FM_DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [WBN_FM_DetailTableViewCell new];
    }
    
    
    WBN_FM_DetailAudioModel *model = [self.arr objectAtIndex:indexPath.row];
    
    cell.titleL.text = model.title;
    cell.dateL.text = model.createTime;
    cell.count = indexPath.row;
    
    NSInteger tempB = 0;
    NSString *mp3Str = [NSString stringWithFormat:@"%@.mp3", model.title];
    for (NSString *tempStr in self.localArr) {
        if ([tempStr isEqualToString:mp3Str]) {
            tempB = 1;
        }
    }

    if (self.buttonIndex == indexPath.row && 1 == self.downLoad) {
        cell.tempNum = 2;
    } else if (1 == tempB) {
        cell.tempNum = 1;
    } else {
        cell.tempNum = 0;
    }
    
    cell.backgroundColor = [UIColor colorWithWhite:0.854 alpha:1.000];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.count != indexPath.row) {
        
        [self.player stop];
        [self.timer invalidate];
        
        self.count = indexPath.row;
        self.currentModel = self.arr[self.count];
        
        self.titleL.text = self.currentModel.title;
        [self getWidth];
        
        [self createPlayer];
    }
}


// 下载
- (void)downLoadFMWith:(NSInteger)num bol:(BOOL)bol
{
    if (YES == bol) {
        
        // 如果下载的不是这首歌则重新下载, 否则继续下载
        if (self.buttonIndex != num) {
            
            self.buttonIndex = num;
            self.downLoadModel = [self.arr objectAtIndex:num];
            
            NSURL *url = [NSURL URLWithString:self.downLoadModel.filePath];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
            self.downLoadT = [session downloadTaskWithRequest:request];
            [self.downLoadT resume];
            self.downLoad = 1;
            
        } else {
            
            [self.downLoadT resume];
            self.downLoad = 1;
            
        }
        
    } else {
        
        [self.downLoadT suspend];
        self.downLoad = 2;
        
    }
    
}
#pragma 下载完毕后调用
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSString *cach = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    // 当前下载的名字
    NSString *name = self.downLoadModel.title;
    NSString *songStr = [NSString stringWithFormat:@"%@.mp3", name];
    
    // 自己给名字 (保存路径)
    NSString *file = [cach stringByAppendingPathComponent:songStr];
    
    NSFileManager *fileM = [NSFileManager defaultManager];
    
    [fileM moveItemAtPath:location.path toPath:file error:nil];
    
    
    self.downLoad = 2;
    
    [self getLocalMusic];
    
    [self.tableV reloadData];
    
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"结束" object:nil];
    
}
#pragma 下载进程
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
//    NSLog(@"本次下载大小:%.2fKB, 已经下载大小:%.2fKB, 总大小:%.2fKB", bytesWritten / 1024.0, totalBytesWritten / 1024.0, totalBytesExpectedToWrite / 1024.0);
    
    // ProgressView
//    CGFloat temp = (totalBytesWritten / 1024.0) / (totalBytesExpectedToWrite  / 1024.0);
//    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"下载" object:[NSString stringWithFormat:@"%f", temp]];
}



- (void)getData
{
    [GoodManNews_AFNetTool getNetWithURL:PLAYURL body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
        self.model = [[WBN_FM_DetailModel alloc] initWithDic:dic];
        
        NSMutableArray *arr = self.model.resourcelist;
        NSMutableArray *arr1 = [WBN_FM_DetailAudioModel baseModelWithArr:arr];
        
        for (NSInteger i = 0; i < arr.count; i++) {
            
            NSDictionary *dic1 = arr[i];
            
            NSMutableArray *tempArr = [dic1 objectForKey:@"audiolist"];
            NSDictionary *tempDic = [tempArr lastObject];
            
            NSString *url = [tempDic objectForKey:@"filePath"];
            
            WBN_FM_DetailAudioModel *tempModel = [arr1 objectAtIndex:i];
            tempModel.filePath = url;
            
            [self.arr addObject:tempModel];
            
        }
        
        self.currentModel = [self.arr firstObject];
        self.count = 0;
        
        [self layoutAudioView];
        [self createPlayer];
        [self.tableV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

// 详情Button
- (void)didCleckButton
{
    CATransition *trans = [CATransition animation];
    trans.type = @"effect";
    trans.duration = 1.f;
    
    [self.view.layer addAnimation:trans forKey:@"bvb"];
    
    self.headV.hidden = YES;
    self.tableV.hidden = YES;
    
    
    self.detailLabel = [UILabel new];
    self.detailLabel.numberOfLines = 0;
    self.detailLabel.font = [UIFont systemFontOfSize:16];
    self.detailLabel.textColor = [UIColor whiteColor];
    self.detailLabel.text = self.model.programDetails;
    [self.view addSubview:self.detailLabel];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.center.mas_equalTo(self.view);
        make.height.equalTo(@300);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        
    }];
    
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tapGR];
    [tapGR release];
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
    CATransition *trans = [CATransition animation];
    trans.type = @"effect";
    trans.duration = 1.f;
    
    [self.view.layer addAnimation:trans forKey:@"bnb"];
    
    self.headV.hidden = NO;
    self.tableV.hidden = NO;
    
    [self.detailLabel removeFromSuperview];
    [self.view removeGestureRecognizer:sender];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
